package cn.tannn.ops.common.message;

import cn.tannn.jdevelops.utils.time.TimeFormatUtil;
import cn.tannn.ops.nginx.entity.Domains;
import lombok.Getter;
import lombok.Setter;

/**
 * ssl 消息通知结构
 *
 * @author <a href="https://t.tannn.cn/">tan</a>
 * @version V1.0
 * @date 2024/8/23 下午3:11
 */
@Getter
@Setter
public class MessageSsl {
    String title;
    String message;
    String domain;
    Long expiryDay;
    String sslPath;
    String timeStr;


    public MessageSsl() {
    }

    public MessageSsl( String message) {
        this.message = message;
    }


    public MessageSsl(String domain, Long expiryDay, String sslPath,String timeStr) {
        this.domain = domain;
        this.expiryDay = expiryDay;
        this.sslPath = sslPath;
        this.timeStr = timeStr;
    }

    public MessageSsl(String message, String domain, Long expiryDay, String sslPath,String timeStr) {
        this.message = message;
        this.domain = domain;
        this.expiryDay = expiryDay;
        this.sslPath = sslPath;
        this.timeStr = timeStr;
    }

    public MessageSsl(Domains domains) {
        this.domain = domains.getDomain();
        this.expiryDay = domains.getExpiryDay();
        this.sslPath = domains.getSslPath();
        if(domains.getExpiryAfter() != null){
            this.timeStr = TimeFormatUtil.date2DefStr(domains.getExpiryAfter());
        }
    }

    @Override
    public String toString(){
        if(expiryDay !=null && domain != null){
            if(expiryDay<=0){
                return """
                       域名: <font color='red'> **%s** </font>,
                       域名证书已过期<font color='Orange'> **%d** </font>天,请尽快处理
                       """.formatted(domain,Math.abs(expiryDay),timeStr);
            }else {
                if(expiryDay<10){
                    return """
                           域名: <font color='red'> **%s** </font>,
                           域名证书有效期还剩<font color='Orange'> **%d** </font>天,
                           将在<font color='red'> **%s** </font>结束,请尽快处理
                           """.formatted(domain,expiryDay,timeStr);
                }else {
                    return """
                           域名: <font color='DarkGreen'> **%s** </font>,
                           域名证书有效期还剩<font color='Orange'> **%d** </font>天,
                           将在<font color='red'> **%s** </font>结束
                           """.formatted(domain,expiryDay,timeStr);
                }

            }
        }else {
            return message;
        }
    }

    public String getTitle() {
        return "https证书到期提醒";
    }
}
